home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Telnet Server 1.0 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-14  |  3.2 KB  |  157 lines  |  [TEXT/KAHL]

  1. /*
  2.     Copyright © 1994 Mikhal Fridberg.
  3.     Telnet - based talk server.    
  4.     this client/server application uses MacTCP to implement a simple talk server.  the server
  5.     opens up several listeners on kGreetingPort (1235).  when a client connects over telnet, the data entered
  6.     in the greeting dialog is sent to the remote connection, server opens 2 windows - one to talk, one to to listen
  7.     and beeps many times. No restrictions for connctions are added. It was a quick hack,
  8.     written for specific application, and it has few bugs that I never fixed, since I
  9.     didn't need it anymore.
  10.     Connection management is done through the use of Operating System queues to simplify tracking
  11.     and usage.
  12. */
  13.  
  14.  
  15. #include <CommResources.h>
  16. #include <Terminals.h>
  17. #include <Connections.h>
  18. #include    <Traps.h>
  19. #include    <ConnectionTools.h>
  20. #include    <CTBUtilities.h>
  21. #include    <TCPPB.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <time.h>
  26. #include <stddef.h>
  27. #include <string.h>
  28. #include <assert.h>
  29.  
  30. #include "const.h"
  31. #include "globals.h"
  32. #include "utils.h"
  33. #include "queues.h"
  34. #include "network.h"
  35. #include "events.h"
  36. #include "interface.h"
  37. #include "main.h"
  38.  
  39. /* main entry point */
  40.  
  41. TermHandle    gTerm, gTermRec;
  42. Ptr            gRecvBuffer;
  43. Ptr            gSendBuffer;
  44. long        gTimeCount = 0;
  45.  
  46. void main(void)
  47. {
  48.     int                err;        
  49.  
  50.     printf(" ");
  51.     InitMac();
  52.     InitQueues();
  53.     InitInterface();
  54.  
  55.     if (InitNetwork()!=noErr)
  56.         ExitToShell();
  57.     if (err=InitCRM() != noErr) {
  58.         CloseNetwork();
  59.         TMDispose(gTerm);
  60.         TMDispose(gTermRec);
  61.         ExitToShell();
  62.         }
  63.     if (err = InitCTBUtilities() != noErr){
  64.         CloseNetwork();
  65.         ExitToShell();
  66.         }
  67.     if (err = InitTM() !=noErr){
  68.         CloseNetwork();
  69.         ExitToShell();    
  70.     }
  71.     gRecvBuffer = NewPtr(kRcvBuffSize);
  72.     gSendBuffer = NewPtr(kRcvBuffSize);
  73.     MainLoop();
  74.     
  75.     CloseNetwork();
  76.     TMDispose(gTerm);
  77.     TMDispose(gTermRec);
  78.     ExitToShell();
  79. }
  80.  
  81. pascal long TermSendProc(Ptr thePtr,long theSize, long refcon, short flags)
  82. {    static    long    savecount;
  83.  
  84. if (gNoSendDataPending) {
  85.     if (theSize > 0) {
  86.         if (memchr(thePtr, '\r', theSize) || memchr(thePtr, '\n', theSize)){
  87.             BlockMove(thePtr, &gSendBuffer[savecount], theSize);
  88.             gSendBuffer[theSize+savecount] = 0x00;
  89.             gNoSendDataPending = FALSE;
  90.             savecount = 0;
  91.             return theSize;
  92.         } else {    
  93.             BlockMove(thePtr, &gSendBuffer[savecount], theSize);
  94.             savecount += theSize;
  95.         return 0L;
  96.         }
  97.         
  98.     }
  99.     return 0L;
  100.     }
  101. }
  102.  
  103.  
  104. pascal OSErr ToolGetConnEnvirons(long refcon, ConnEnvironRec *theEnvirons)
  105. {
  106. }
  107. /*    initialize macintosh managers and some globals */
  108.  
  109. void InitMac(void)
  110. {
  111.     SysEnvRec envRec;
  112.     
  113.     InitGraf(&qd.thePort);
  114.     InitFonts();
  115.     InitWindows();
  116.     InitMenus();
  117.     TEInit();
  118.     InitDialogs(nil);
  119.     InitCursor();
  120.     FlushEvents(everyEvent,0);
  121.     
  122.     if (SysEnvirons(1,&envRec)!=noErr)
  123.         gRunningSeven = false;
  124.     else
  125.         gRunningSeven = (envRec.systemVersion >= 0x700);
  126.     
  127.     if (gRunningSeven)
  128.         GetCurrentProcess(&gOurPSN);
  129. }
  130.  
  131.  
  132. /*    main event loop.  note that we use a *very* large sleeptime if we're running under System 7
  133. */
  134.  
  135. #define    GetSleepTime     100
  136.  
  137. void MainLoop(void)
  138. {
  139.     EventRecord ev;
  140.     MyQElemPtr iopb;
  141.     long    len;
  142.     int        err;
  143.     CMFlags    flags = 0L;
  144.  
  145.     while (!gDone) {
  146.         if (WaitNextEvent(everyEvent,&ev,GetSleepTime,nil)) {
  147.             HandleEvent(&ev);
  148.             HandleIdleTime(&ev);
  149.             
  150.  
  151.         }
  152.         else HandleIdleTime(&ev);
  153.         UpdateNumberList();
  154.     }
  155. }
  156.  
  157.